package com.sbx.controller;


import com.sbx.entity.dto.FileOB;
import com.sbx.service.FilesService;
import com.sbx.utils.Result;
import com.sbx.validations.NotRequiredField;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

@Api(tags = "文件管理")
@RequestMapping("files")
@RestController
public class FilesController {

    @Resource
    private FilesService filesService;


    @GetMapping("fileList")
    @ApiOperation("获取文件列表")
    public Result fileList(@RequestParam String id,
                           @RequestParam(defaultValue = "false", required = false) Boolean self,
                           @RequestParam(defaultValue = "1", required = false) Integer pageIndex,
                           @RequestParam(defaultValue = "10", required = false) Integer pageSize) {
        return Result.page(filesService.fileList(id, self, pageIndex, pageSize));
    }

    @PostMapping
    @ApiOperation("上传文件")
    public Result uploadFile(@RequestPart("file") MultipartFile file,
                             @RequestParam(defaultValue = "public") String visit,
                             @RequestParam String parentId,

                             @RequestParam String name,
                             @RequestParam String bucket) {
        filesService.uploadFile(file, visit, parentId, name, bucket);
        return Result.success();
    }

    @GetMapping
    @ApiOperation("下载文件")
    public Result downFile(@RequestParam("id") String id, HttpServletResponse response) {
        filesService.downFile(id, response);
        return Result.success();
    }

    @PostMapping("dir")
    @ApiOperation("创建文件夹")
    public Result createDir(FileOB fileOB) {
        filesService.createDir(fileOB);
        return Result.success();
    }

    @DeleteMapping("{id}")
    @ApiOperation("删除文件(夹)")
    public Result delFileOrDir(@PathVariable String id) {
        filesService.delFileOrDir(id);
        return Result.success();
    }

    @PutMapping("{id}")
    @ApiOperation("修改文件(夹)名称")
    public Result putFileOrDir(@PathVariable("id") String id,
                               @RequestParam(required = false) String name,
                               @RequestParam(required = false) String visit) {
        filesService.putFileOrDir(id, name, visit);
        return Result.success();
    }


    @PostMapping("{originId}/cp")
    @ApiOperation("复制文件")
    public Result cpFile() {
        filesService.cpFile();
        return Result.success();
    }


}
